package action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.New;
import model.NewType;
import model.Position;
import model.User;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import service.NewService;
import service.NewTypeService;
import service.UserService;
import util.DataUtil;
import util.ImageUtil;

public class NewAction extends ActionSupport implements RequestAware,
ModelDriven<New>, Preparable{
	private static final long serialVersionUID = 7503387777201286274L;
	private NewService newService;
	private NewTypeService newTypeService;
	private UserService userService;
	private Integer nid;
	private Integer uid;
	private String title;
	private Integer id;
	private String content;
	private User user;
	private Integer newType;
	private String s;
	
	
	
	public void setS(String s) {
		this.s = s;
	}



	public void setContent(String content) {
		this.content = content;
	}

	
	
	public void setId(Integer id) {
		this.id = id;
	}
	
	
	public void setUser(User user) {
		this.user = user;
	}

	
	
	public void setNewType(Integer newType) {
		this.newType = newType;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setNid(Integer nid) {
		this.nid = nid;
	}

	public void setUid(Integer uid) {
		this.uid = uid;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setNewTypeService(NewTypeService newTypeService) {
		this.newTypeService = newTypeService;
	}

	public void setNewService(NewService newService) {
		this.newService = newService;
	}

	/**
	 * 显示新闻编辑页面
	 */
	public String input(){
		return INPUT;
	}
	
	public String save(){
		NewType n = new NewType();
		n.setId(newType);
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		User user = (User) session.getValue("user");
		
		if( newType == 1 ){
			model.setTest("sh");
		}
		if( newType == 2 ){
			model.setTest("yl");
		}
		if( newType == 3 ){
			model.setTest("js");
		}
		if( newType == 4 ){
			model.setTest("ty");
		}
		
		model.setCreateDate(new Date());
		model.setIsTest(0);
		model.setUser(user);
		model.setNewtype(n);
		newService.saveOrUpdate(model);
		return "save1";
	}
	
	
	public void prepareSave(){
		model = new New();
	}
	
	public void prepareInput() {
		request.put("news", newTypeService.getAll());
	}
	
	
	/**
	 * 审核页面
	 */
	
	public String list(){
		request.put("news", newService.getTest());
		return "list";
	}
	
	public String doList(){
		model = newService.get(id);
		model.setIsTest(1);
		newService.saveOrUpdate(model);
		request.put("news", newService.getTest());
		return "list";
	}
	
	/**
	 *首页 
	 */

	public String index(){
		return "index";
	}
	
	public void prepareIndex(){ 
		request.put("sh", newService.getT1());
		request.put("yl", newService.getT2());
		request.put("js", newService.getT3());
		request.put("ty", newService.getT4());
	}
	
	public String show(){
		
		return "show";
	}
	
	public void prepareShow(){
		System.out.println(id);
		request.put("con", newService.getById(id));
	}
	
	
	public String sh(){
		System.out.println(s);
		return "cate";
	}
	
	public void prepareSh(){
		request.put("cate", newService.getT(s));
	}
	
	
	public String yl(){
		
		return "cate";
	}
	
	public void prepareYl(){
		request.put("cate", newService.getT(s));
	}
	
	public String js(){
		
		return "cate";
	}
	
	public void prepareJs(){
		request.put("cate", newService.getT(s));
	}
	
	public String ty(){
		
		return "cate";
	}
	
	public void prepareTy(){
		request.put("cate", newService.getT(s));
	}
	
	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub	
	}

	private New model;
	
	@Override
	public New getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	private Map<String, Object> request;
	
	@Override
	public void setRequest(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}
	

}
